<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
   /*  // Object.defineProperty
    const data = {
      a: 10,
      b: 20
    }
    let a = 100;
    Object.defineProperty(data, "a", {
      // getter 函数，劫持 这个属性 取值 劫持到 函数return值
      get(){
        console.log('getter触发了');
        return a;
      },
      // setter 劫持 赋值操作 
      set(val){
        console.log('setter触发', val);
        a = val;
      }
    })
    console.log(data.a);
    data.a = 500;
    console.log(data.a); */

    // 数据代理 proxy 对于整个对象做代理
    const data = {
      a: 10,
      b: 20
    }
    const proxyData = new Proxy(data, {
      // getter 取值
      get(target, key){
        console.log('getter触发', target, key);
        return target[key]
      },
      set(target, key, val){
        console.log('setter触发', target, key, val);
        target[key] = val;
      }
    })

    console.log(proxyData.a);
    proxyData.a = 100;
    console.log(proxyData.a);
  </script>
</body>
</html>